/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package RRHH_controlador;

import CONTABILIDAD_MODELO.CON_DATOS_EMPRESA;
import Conexion.Conexion;
import Modelo.mail;
import RRHH_Modelo.RHH_CARGO;
import RRHH_Modelo.RHH_FECHAS_HITO;
import RRHH_Modelo.RHH_PERSONA;
import RRHH_Modelo.RHH_USUARIO;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author equipo_2
 */
@WebServlet(name = "RRHH_REENVIO_CONTRASENA_CONTROLLER", urlPatterns = {"/RRHH_REENVIO_CONTRASENA_CONTROLLER"})
public class RRHH_REENVIO_CONTRASENA_CONTROLLER extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet RRHH_REENVIO_CONTRASENA_CONTROLLER</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet RRHH_REENVIO_CONTRASENA_CONTROLLER at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
     protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            RHH_USUARIO usuario = ((RHH_USUARIO)request.getSession().getAttribute("usr"));
            Conexion con = new Conexion(usuario.getUSUARIO(), usuario.getPASSWORD());
            
            String evento = request.getParameter("proceso");
            String html="";
            
            switch(evento)
            {
                case "buscarEmpleado":
                    html=buscarEmpleado(request,con);
                    break;
                case "enviar":
                    html=suspenderempleado(request,con);
                    break;
               
            }
            
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/plain");
            response.getWriter().write(html);
        } catch (SQLException ex) {

        }
    }
    
    private String buscarEmpleado(HttpServletRequest request, Conexion con) throws SQLException {
        String text=request.getParameter("text");
        RHH_PERSONA persona=new RHH_PERSONA(con);
        RHH_USUARIO usuario=new RHH_USUARIO(con);
        List<RHH_USUARIO> lista=usuario.buscarempleado(text);
        String html="";
        RHH_CARGO cargo=new RHH_CARGO(con);
        RHH_FECHAS_HITO hito=new RHH_FECHAS_HITO(con);
        for (int i = 0; i < lista.size(); i++) {
            persona=persona.BuscarxID(lista.get(i).getID_PERSONA());
            String src="../img/imagen3.jpg";
            String nombre=persona.getPRIMER_NOMBRE()+" ";
            if(persona.getSEGUNDO_NOMBRE()!=null || !persona.getSEGUNDO_NOMBRE().equals("null")){
                nombre+=persona.getSEGUNDO_NOMBRE()+" ";
            }
            nombre+=persona.getPRIMER_APELLIDO();
            if(persona.getSEGUNDO_APELLIDO()!=null || !persona.getSEGUNDO_APELLIDO().equals("null")){
                nombre+=" "+persona.getSEGUNDO_APELLIDO()+" ";
            }
           
            String color="white";
            if(lista.get(i).getESTADO()==1){
                color="red";
            }
            cargo=cargo.BuscarXid(lista.get(i).getID_CARGO());
             html+="<div  onclick=\"reenviocontraseña(this,'"+persona.getCI()+"','"+nombre+"','"+persona.getEMAIL()+"')\" style=\"padding: 5px; width: 140px; background:"+color+"; border-radius: 7px; height: 163px; margin: 5px; float:left;\">\n" +
                        "<div class=\"centrado negrilla\" style=\"height: 42px;\" >"+cargo.getDESCRIPCION()+"</div>\n" +
                        "<div class='centrado' style=\"height: 80px; width: 104px;\">\n" +
                            "<img style=\"width: 100%; height: 100%;\" alt=\"\" class='imagen' data-ci='" + persona.getCI()+ "' data-foto='"+persona.getFOTO()+"' src=\""+src+"\">\n" +
                        "</div>\n" +
                        "<div class=\"centrado negrilla\">"+persona.getCI()+"</div>\n" +
                        "<div class=\"centrado\">"+nombre+"</div>"
                    + "</div>";
        }
        return html;
    }
    private String suspenderempleado(HttpServletRequest request, Conexion con) throws SQLException, IOException{
        int ci = Integer.parseInt(request.getParameter("id"));
        con.Transacction();
        RHH_PERSONA per = new RHH_PERSONA(con).Buscar(ci+"");
        RHH_USUARIO usuario=new RHH_USUARIO(con).BuscarXidPersona(per.getID());
        

        String[] letras = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
                          "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        String pass="";
        int letra = (int)(Math.random()*52);
        pass+=letras[letra];
        letra = (int)(Math.random()*52);
        pass+=letras[letra];
        letra = (int)(Math.random()*52);
        pass+=letras[letra];
        letra = (int)(Math.random()*52);
        pass+=letras[letra];
        letra = (int)(Math.random()*52);
        pass+=letras[letra];
        letra = (int)(Math.random()*52);
        pass+=letras[letra];
        letra = (int)(Math.random()*52);
        pass+=letras[letra];

        usuario.ModificarUSR(usuario.getID_CARGO(), usuario.getUSUARIO(), pass, usuario.getESTADO());
        CON_DATOS_EMPRESA emp = new CON_DATOS_EMPRESA(con);
        emp.setCon(con);
        emp=emp.Buscar();
        mail me = new mail(per.getEMAIL(), "CAMBIO PASSWORD ", per.getCI()+"", pass, per.getPRIMER_APELLIDO(),emp.getRAZON_SOCIAL());
        con.commit();
        con.Close();
        return "Su contraceña a sido reenviada a "+per.getEMAIL();
    }
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
